package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.FoodDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FoodDaoImpl implements FoodDao {
    private Connection con;
    private PreparedStatement pre;
    @Override
    public List<Food> listFoodByBusinessId(int businessId) throws SQLException {
        List<Food> listFood = new ArrayList<>();
        String sql = "select * from food where businessId=? order by foodId";
        try {
            //1.连接数据库
            con = DBUtil.getConnection();
            //2.获取到预处理对象
            pre = con.prepareStatement(sql);
            pre.setInt(1, businessId);
            //3.执行查询
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                Food food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));
                listFood.add(food);
            }
        }  finally {
            DBUtil.close(pre); //关闭资源
        }
        return listFood;
    }
}
